const fs = require("fs");
const path = require("path");

let obj = []

function formatTime(time) {
    let Time = new Date(time);
    let year = Time.getFullYear();
    let mounth = Time.getMonth() + 1;
    let date = Time.getDate();
    let hour = Time.getHours();
    let minite = Time.getMinutes();
    return `${year}/${mounth}/${date} ${hour}:${minite}`
}

function readFileInfo(Path , obj) {
    if(fs.statSync(Path).isDirectory()) {
        let fileNameList = fs.readdirSync(Path);
        fileNameList.forEach((item , index) => {
            obj[index] = {};
            let statInfo = fs.statSync(path.join(Path , item));
            obj[index].fileSize = statInfo.size;
            obj[index].fileTime = formatTime(statInfo.ctime);
            obj[index].fileName = item;
            obj[index].filePath = path.join(Path , item);
            if(fs.statSync(path.join(Path , item)).isFile()) {
                obj[index].fileType = "file";
            } else if(fs.statSync(path.join(Path , item)).isDirectory()) {
                obj[index].fileType = "folder";
                obj[index].list = [];
                readFileInfo(path.join(Path , item) , obj[index].list)
            }
        })
    }
}

console.log(__dirname);
let Path = "/home/wangding/node.js-listen"
readFileInfo(Path , obj)
console.log(obj);
console.log(JSON.stringify(obj));

fs.writeFile(path.join(__dirname , "data.json") , JSON.stringify(obj , null , 2) , () => {
    console.log("文件写入完成");
})